package com.reran.geek.demo.Week03.homework1;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @description:
 * @author: WangSiTeng
 * @date: 2021/11/14
 */
public class HttpClientUtil {
    public static CloseableHttpClient httpClient = HttpClients.createDefault();

    public static void main(String[] args) throws IOException {
        String url = "http://localhost:8801";
//        String url = "http://www.baidu.com";
        String responseString = HttpClientGet(url);
        System.out.println("HttpClientUtil call " + url + " response: " + responseString);

    }

    public static String HttpClientGet(String url) throws IOException {
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response1 = httpClient.execute(httpGet);
        try {
            System.out.println(response1.getStatusLine());
            HttpEntity entity1 = response1.getEntity();
            return EntityUtils.toString(entity1, "UTF-8");
        } finally {
            response1.close();
        }
    }
}
